﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Abp.Dependency;
using Castle.Core.Logging;
using Newtonsoft.Json;

namespace Abp.Rebus.Job
{
    public class JobHelper
    {
        public static void SendRequest(string callBackUrl)
        {
            var logger = IocManager.Instance.Resolve<ILogger>();

            using (HttpClient client = new HttpClient())
            {
                // logger.Info("callBack url =" + callBackUrl);
                client.BaseAddress = new Uri(callBackUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                HttpResponseMessage response = client.GetAsync(callBackUrl).Result;
                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync().Result;

                    if (!string.IsNullOrEmpty(result) && result.StartsWith("{\"success\""))
                    {
                        var obj = JsonConvert.DeserializeObject<AbpResponseResult>(result);
                        if (obj != null && !obj.Success)
                        {
                            throw new Exception(result);
                        }

                    }
                }
                else
                {
                    throw new Exception(response.Content.ReadAsStringAsync().Result);
                }

            }

        }
    }


    public class AbpResponseResult
    {
        /// <summary>
        /// Indicates success status of the result.
        /// Set <see cref="Error"/> if this value is false.
        /// </summary>
        public bool Success { get; set; }



        /// <summary>
        /// This property can be used to indicate that the current user has no privilege to perform this request.
        /// </summary>
        public bool UnAuthorizedRequest { get; set; }
    }



}
